package org.quickserver.net.Shengshan.control;


import com.dykj.fireFighting.web.model.report.DevHisSs;
import com.dykj.fireFighting.web.utils.UUIDUtils;
import org.quickserver.net.model.ShengShanServerData;
import org.quickserver.net.server.ClientHandler;
import org.quickserver.util.data.dataProcessing;
import org.quickserver.util.state.SS254State;

import java.text.DecimalFormat;
import java.util.*;

public class SS254 {
    public SS254()
    {
    }
    public static DevHisSs Shengshan254OBJ(ClientHandler handler, String strs[])
    {
        DevHisSs devHisSs = new DevHisSs();
        try{
            System.out.println("+++"+Arrays.toString(strs));
            ShengShanServerData ec = (ShengShanServerData)handler.getClientData();//获取连接数据
            int strLength=dataProcessing.hexStringToInt10(strs[0]); //数据长度

            String kzm=strs[1]; //控制码应解析    转二进制

            devHisSs.setZtfssj(new Date());

            /**
             * IMEI
             */
            String uStr[] = dataProcessing.strsToStrs(strs, 2, 17);
            byte aa[]=dataProcessing.hexStringsToByteArray(uStr);
            String uniqueCode = dataProcessing.gB18030ByteArrayToUtf8String(aa);
            uniqueCode = uniqueCode.trim();
            ec.setUniqueCode(uniqueCode);
            devHisSs.setId(UUIDUtils.get32UUID());
            devHisSs.setDevCode(uniqueCode);
            devHisSs.setRelationId(uniqueCode);
            String sblx=strs[18]; //设备类型
            devHisSs.setSblx(dataProcessing.strTo10StrAll(sblx));

            String DI1=strs[19];    //0x00：漏电+温度+单相 0x01：漏电+温度+单相+电能 0x10：漏电+温度+三相 0x11：漏电+温度+三相+电能 bit0: 0：没有电能， 1：有电能 bit1:0：不含谐波，1：含谐波 bit4: 0：单相， 1：三相 bit5:0~2DI, 1~4DI
            String descripForDI="";
            if("00".equals(DI1)){
                descripForDI="漏电+温度+单相";
            } else if("01".equals(DI1)){
                descripForDI="漏电+温度+单相+电能";
            }else if("10".equals(DI1)){
                descripForDI="漏电+温度+三相";
            }else if("11".equals(DI1)){
                descripForDI="漏电+温度+三相+电能";
            }else{
            }
            devHisSs.setDi1(descripForDI);

            String  Ir_ch=strs[20];  //漏电通道（0-1）
            devHisSs.setIrCh(dataProcessing.strTo10StrAll(Ir_ch));
            String  T_ch=strs[21];  //温度通道（0-4）
            devHisSs.setTCh(dataProcessing.strTo10StrAll(T_ch));


            String bjw=strs[22]+strs[23];//报警位
            String componentState[] = new String[2];
            componentState[0] = strs[22];
            componentState[1] = strs[23];
            Map<String, String> map = SS254State.getSS254State(componentState,"Alarmbit");
            if(null !=map && map.size()>0){
                Map<String, String> codeAndJx = SS254State.getCodeAndJx(map);
                devHisSs.setBjzt(codeAndJx.get("codes"));
                devHisSs.setBjztSm(codeAndJx.get("jx"));
            }
            devHisSs.setBjw(dataProcessing.strTo10StrAll(bjw));

            String gzw=strs[24]+strs[25];//故障位
            componentState[0]=strs[24];
            componentState[1]=strs[25];
            Map<String, String> faultbit = SS254State.getSS254State(componentState, "Faultbit");
            if(null !=faultbit && faultbit.size()>0){
                Map<String, String> codeAndJx = SS254State.getCodeAndJx(faultbit);
                devHisSs.setGzw(codeAndJx.get("codes"));
                devHisSs.setGzwJx(codeAndJx.get("jx"));
            }

            String snw=strs[26]+strs[27];//使能位
            componentState[0]=strs[26];
            componentState[1]=strs[27];
            Map<String, String> linkbit = SS254State.getSS254State(componentState, "Linkbit");
            if(null !=linkbit && linkbit.size()>0){
                Map<String, String> codeAndJx = SS254State.getCodeAndJx(linkbit);
                devHisSs.setSnw(codeAndJx.get("codes"));
                devHisSs.setSnwJx(codeAndJx.get("jx"));
            }

            String dlgjw=strs[28]+strs[29];//电量告警位
            componentState[0]=strs[28];
            componentState[1]=strs[29];
            Map<String, String> limitbit = SS254State.getSS254State(componentState, "Limitbit");
            if(null !=limitbit && limitbit.size()>0){
                Map<String, String> codeAndJx = SS254State.getCodeAndJx(limitbit);
                devHisSs.setDlgjw(codeAndJx.get("codes"));
                devHisSs.setDlgjwJx(codeAndJx.get("jx"));
            }

            String sydl=strs[30]+strs[31];//剩余电流
            devHisSs.setSydl(dataProcessing.strTo10StrAll(sydl));
            /**
             * 温度直接转十进制
             */
            String d1lwd=strs[32]+strs[33];//第一路温度值
            devHisSs.setD1lwd(dataProcessing.hexStringToInt10(d1lwd));
            String d2lwd=strs[34]+strs[35];//第二路温度值
            devHisSs.setD2lwd(dataProcessing.hexStringToInt10(d2lwd));
            String d3lwd=strs[36]+strs[37];//第三路温度值
            devHisSs.setD3lwd(dataProcessing.hexStringToInt10(d3lwd));
            String d4lwd=strs[38]+strs[39];//第四路温度值
            devHisSs.setD4lwd(dataProcessing.hexStringToInt10(d4lwd));

            String dybb=strs[40]+strs[41]+strs[42]+strs[43];//电压变比PT
            Float pt=dataProcessing.hexToFloat(dybb);
            //devHisSs.setDybb(dataProcessing.strTo10StrAll(dybb));
            devHisSs.setDybb(pt+"");

            String dlbb=strs[44]+strs[45]+strs[46]+strs[47];//电流变比CT
            Float ct=dataProcessing.hexToFloat(dlbb);
            /*devHisSs.setDlbb(dataProcessing.strTo10StrAll(dlbb));*/
            devHisSs.setDlbb(ct+"");

            DecimalFormat df = new DecimalFormat("#0.00");

            String UA=strs[48]+strs[49];
            double ua=dataProcessing.getUdisp(dataProcessing.sumHexStringToInt10(strs[48], strs[49]),pt);
            ua = ua * 0.1;
            devHisSs.setUa(df.format(ua));

            String UB=strs[50]+strs[51];
            double ub=dataProcessing.getUdisp(dataProcessing.sumHexStringToInt10(strs[50], strs[51]),pt);
            ub = ub * 0.1;
            devHisSs.setUb(df.format(ub));

            String UC=strs[52]+strs[53];
            double uc=dataProcessing.getUdisp(dataProcessing.sumHexStringToInt10(strs[52],strs[53]),pt);
            uc = uc * 0.1;
            devHisSs.setUc(df.format(uc));

            String IA=strs[54]+strs[55];
            double ia = dataProcessing.getIdisp(dataProcessing.sumHexStringToInt10(strs[54], strs[55]), ct);
            ia = ia * 0.001;
            devHisSs.setIa(df.format(ia));

            String IB=strs[56]+strs[57];
            double ib = dataProcessing.getIdisp(dataProcessing.sumHexStringToInt10(strs[56], strs[57]), ct);
            ib = ib * 0.001;
            devHisSs.setIb(df.format(ib));

            String IC=strs[58]+strs[59];
            double ic = dataProcessing.getIdisp(dataProcessing.sumHexStringToInt10(strs[58], strs[59]), ct);
            ic = ic * 0.001;
            devHisSs.setIc(df.format(ic));

            String P=strs[60]+strs[61];
            devHisSs.setP(dataProcessing.strTo10StrAll(P));

            String PF=strs[62]+strs[63];
            devHisSs.setPf(dataProcessing.strTo10StrAll(PF));

            String EP=strs[64]+strs[65]+strs[66]+strs[67];//EP
            devHisSs.setEp(dataProcessing.strTo10StrAll(EP));
        }catch (Exception e){
            e.printStackTrace();
        }
        return devHisSs;
    }
}
